home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
dbwrendr.zip
/
SOURCE
/
RAY.C
< prev
next >
Wrap
Text File
|
1989-05-14
|
21KB
|
594 lines
/************************************************************************
* *
* Copyright (c) 1987, David B. Wecker *
* All Rights Reserved *
* *
* This file is part of DBW_Render *
* *
* DBW_Render is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY. No author or distributor accepts *
* responsibility to anyone for the consequences of using it or for *
* whether it serves any particular purpose or works at all, unless *
* he says so in writing. Refer to the DBW_Render General Public *
* License for full details. *
* *
* Everyone is granted permission to copy, modify and redistribute *
* DBW_Render, but only under the conditions described in the *
* DBW_Render General Public License. A copy of this license is *
* supposed to have been given to you along with DBW_Render so you *
* can know your rights and responsibilities. It should be in a file *
* named COPYING. Among other things, the copyright notice and this *
* notice must be preserved on all copies. *
************************************************************************
* *
* Authors: *
* DBW - David B. Wecker *
* JHL - John H. Lowery, IBM-PC/Microsoft C port *
* *
* Versions: *
* V1.0 870125 DBW - First released version *
* V1.1 (IBM) 890101 JHL - version 1.0 for the IBM-PC/VGA/MCGA *
* *
************************************************************************/
#define MODULE_RAY
#include "ray.h"
vector base,
vx,
vy,
d,
p,
sumval,
sampleval,
varsum,
samplep,
rnd_vr,
rnd_vu,
subvr,
subvu,
old_ave,
ave_val,
eye2,
focalpoint,
lens1,
lens2,
temp1,
temp2;
float rnd_wid,
rnd_hi,
lensx,
lensy,
focallength,
totalcount,
count,
maxcount,
d_alias,
d_bestf1,
d_bestf2;
unsigned modulo;
int row,
col,
i,j,k,
compthresh,
compmin,
bunch,
fac1,
fac2,
bestf1,
bestf2,
spatialx,
spatialy,
pixelcompute,
pixelguess,
didguess;
long curtime,
prvtime,
nxttime,
xtrtime,
buftime,
totaltime,
totalguess,
totalcompute;
unsigned char cache[9][MAXX][3];
int computes[MAXX];
guess2(col,sum,val,dis,xdir,ydir)
int col,*sum,dis,xdir,ydir;
int val[3];
{
int i,x1,y1,x2,y2,retval,shft,isum;
shft = 6 - dis;
isum = 1 << shft;
y1 = 4 - (dis * ydir);
x1 = col - (dis * xdir);
if (y1 < 0)
y1 = 0;
if (y1 > 8)
y1 = 8;
if (x1 < 0)
x1 = 0;
if (x1 >= MAXCOL)
x1 = MAXCOL-1;
retval = 0;
y2 = y1;
while (1)
{
if (cache[y2][x1][0] != 0xFF)
{
for (i = 0; i < 3; i++)
val[i] += ((int)cache[y2][x1][i]) << shft;
*sum += isum;
retval = 1;
}
if (y2 == 4)
break;
y2 += ydir;
}
x2 = x1;
while (1)
{
if (cache[y1][x2][0] != 0xFF)
{
for (i = 0; i < 3; i++)
val[i] += ((int)cache[y1][x2][i]) << shft;
*sum += isum;
retval = 1;
}
if (x2 == col)
break;
x2 += xdir;
}
return(retval);
}
void guess(col)
int col;
{
int i,sum,dir,dis;
int val[3];
sum = 0;
dir = 0;
for (i = 0; i < 3; i++)
val[i] = 0;
for (dis = 1; dir != 15 && dis < 7; dis++)
{
if ((dir & 1) != 1)
if (guess2(col,&sum,val,dis,-1,1))
dir |= 1;
if ((dir & 2) != 2)
if (guess2(col,&sum,val,dis,1,-1))
dir |= 2;
if ((dir & 4) != 4)
if (guess2(col,&sum,val,dis,1,1))
dir |= 4;
if ((dir & 8) != 8)
if (guess2(col,&sum,val,dis,-1,-1))
dir |= 8;
if (dis > 2 && ((dir & 3) == 3 || (dir & 12) == 12))
break;
}
if (sum == 0)
sum = 1;
for (i = 0; i < 3; i++)
{
cache[4][col][i] = (unsigned char)(val[i] / sum);
}
}
void do_raytrace()
{
/* set up pixel coordinate */
vecscale((float)col,vr,vx);
vecsub(base,vy,p);
vecsum(p,vx,p);
/* Compute this next pixel. Distribute 'n' rays to do this */
veczero(sumval); /* start with a black pixel */
veczero(varsum);
veccopy(backgroundval,ave_val); /* have to start someplace */
count = 0.0;
bunch = antialias;
spatialx = 0;
spatialy = 0;
curr_runs = 0;
while (bunch > 0)
{
/* Cast another sample ray for this pixel into the next subregion */
/* Pick a new random perturbing vector for this sample for spatial
antialiasing */
if (count < d_alias)
{
rnd_wid = 0.5; /* force first bunch through region centers */
rnd_hi = 0.5;
}
else
{
rnd_wid = rnd();
rnd_hi = rnd();
}
vecscale(rnd_wid / d_bestf2,vr,rnd_vr);
vecscale(rnd_hi / d_bestf1,vu,rnd_vu);
vecscale((float) spatialx / d_bestf2,vr,subvr);
vecscale((float) spatialy / d_bestf1,vu,subvu);
vecsum(rnd_vr,subvr,samplep);
vecsum(samplep,p,samplep);
vecsub(samplep,rnd_vu,samplep);
vecsub(samplep,subvu,samplep);
veccopy(eye,eye2);
direction(eye2,samplep,d); /* direction from lens center to pixel */
if (aperture > 0.0)
{
vecscale(focus,d,focalpoint); /* relative point on focus plane */
lensx = rnd(); /* pick random point on lens */
lensy = rnd();
if (rnd() < 0.5)
lensx = -lensx;
if (rnd() < 0.5)
lensy = -lensy;
vecscale(lensx,lens1,temp1); /* make into vectors in lens plane */
vecscale(lensy,lens2,temp2);
vecsum(temp1,temp2,eye2); /* point in the lens */
direction(eye2,focalpoint,d); /* from spot on lens to focal point */
vecsum(eye,eye2,eye2); /* calc new 3D eye point in lens */
}
/* allow aborting if curr_runs > max_runs */
if (setjmp(env))
{
computes[col] = max_runs; /* too many computes */
cache[8][col][1] = 0; /* make sure we don't try again */
return;
}
/*